package com.fly.redis.config

import org.springframework.cache.annotation.EnableCaching
import org.springframework.context.annotation.Bean
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory
import org.springframework.data.redis.core.ReactiveRedisTemplate
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
import org.springframework.data.redis.serializer.RedisSerializationContext
import org.springframework.data.redis.serializer.StringRedisSerializer

/**
 * @author  fly
 * @date  2021/5/18 17:08
 * @version 1.0
 */
@EnableCaching
class RedisAutoConfigure {

    @Bean
    fun reactiveJsonPostRedisTemplate(redisConnectionFactory: ReactiveRedisConnectionFactory): ReactiveRedisTemplate<String, Any> =
        ReactiveRedisTemplate(redisConnectionFactory, RedisSerializationContext.newSerializationContext<String, Any>(StringRedisSerializer())
            .hashKey(StringRedisSerializer())
            .hashValue(Jackson2JsonRedisSerializer(Any::class.java))
            .build())
}